package com.restaurant.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.restaurant.domain.Menu;
import com.restaurant.domain.Menumodifires;
import com.restaurant.domain.Modifiremaster;
import com.restaurant.service.MenuService;
import com.restaurant.service.MenumodifiresService;
import com.restaurant.service.ModifiremasterService;

@Component
@RequestMapping("/action/menumodifire.do")
@SessionAttributes({"menu","modifier"})
public class MenuModifireFormController {
	
	private final String loadAllMenu = "from Menu where isdeleted = false";
	
	@Autowired
	private MenuService objMenuService;
	
	@Autowired
	private ModifiremasterService objModifiremasterService;
	
	@Autowired
	private MenumodifiresService objMenumodifiresService;
	
	
	@RequestMapping(method=RequestMethod.GET)
	public String menuModifireGETRequest(ModelMap map){
		ArrayList<Menu> listMenus = null;
		Menu menu = new Menu();
		try {
			listMenus = (ArrayList<Menu>) objMenuService.loadByHql(loadAllMenu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("listMenus", listMenus);
		map.addAttribute("menu", menu);
		return "MenuModifire";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String menuModifirePOSTRequest(ModelMap map, @ModelAttribute ArrayList<Menu> listMenus, @ModelAttribute Menu menu, BindingResult result, HttpServletRequest request, HttpServletResponse response){
		String action = request.getParameter("action");
		
		if(action.compareTo("onselect")==0){
			if(menu.getMenuID()!=0)
				map = onSelectMenu(map, menu, request, response);
			else
				return "redirect:menumodifire.do";
		}else if(action.compareTo("edit")==0){
			map = onAddMenuModifire(map, menu, request, response);
		}else if(action.compareTo("del")==0){
			map = onDeleteMenuModifire(map, menu, request, response);
		}
		
		try {
			listMenus = (ArrayList<Menu>) objMenuService.loadByHql(loadAllMenu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("listMenus", listMenus);
		return "MenuModifire";
	}
	
	private ModelMap onSelectMenu(ModelMap map, Menu menu, HttpServletRequest request, HttpServletResponse response){
		String menuID = menu.getMenuID().toString();		
		
		String queryLoadModifireForMenuID = "from Menumodifires where menu.menuID = " + menuID;
		
		ArrayList<Menumodifires> listMenumodifires = null;
		try {
			listMenumodifires = (ArrayList<Menumodifires>) objMenumodifiresService.loadByHql(queryLoadModifireForMenuID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		StringBuffer modifireIDs = new StringBuffer("(");
		for(int i=0;i<listMenumodifires.size();i++){
			modifireIDs.append(listMenumodifires.get(i).getModifiremaster().getModifireID());
			modifireIDs.append(",");
		}
		if(modifireIDs.lastIndexOf(",")>0)
			modifireIDs.deleteCharAt(modifireIDs.lastIndexOf(","));
		else
			modifireIDs.append("-1");
		modifireIDs.append(")");
		
		String queryNotMenuModifires = "from Modifiremaster where isdeleted = false and modifireID not in " + modifireIDs;
		
		ArrayList<Modifiremaster> listNotModifiremasters = null;
		
		try {
			listNotModifiremasters = (ArrayList<Modifiremaster>) objModifiremasterService.loadByHql(queryNotMenuModifires);
		} catch (Exception e) {
			e.printStackTrace();	
		}
		
		map.addAttribute("listMenumodifires", listMenumodifires);
		map.addAttribute("listNotModifiremasters", listNotModifiremasters);
		map.addAttribute("menu", menu);
		return map;
	}
	
	private ModelMap onAddMenuModifire (ModelMap map, Menu menu, HttpServletRequest request, HttpServletResponse response){
		Long menuID = menu.getMenuID();
		
		StringBuffer ids = new StringBuffer(request.getParameter("ids"));
		ids.deleteCharAt(ids.lastIndexOf(","));
		ids.insert(0, "(");
		ids.insert(ids.length(), ")");
		
		ArrayList<Modifiremaster> listModifiremasters = null;
		try {
			listModifiremasters = (ArrayList<Modifiremaster>) objModifiremasterService.loadByHql("from Modifiremaster where modifireID in " + ids.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Menumodifires menumodifires = null;
		
		for (Modifiremaster modifiremaster : listModifiremasters) {
			menumodifires = new Menumodifires();
			menumodifires.setIsdeleted(false);
			menumodifires.setModifiremaster(modifiremaster);
			try {
				menumodifires.setMenu(objMenuService.loadByPrimaryKey(menuID));
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				objMenumodifiresService.addOrModify(menumodifires);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return onSelectMenu(map, menu, request, response);
	}
	
	private ModelMap onDeleteMenuModifire(ModelMap map, Menu menu, HttpServletRequest request, HttpServletResponse response){
		
		StringBuffer ids = new StringBuffer(request.getParameter("ids"));
		ids.deleteCharAt(ids.lastIndexOf(","));
		ids.insert(0, "(");
		ids.insert(ids.length(), ")");
		
		ArrayList<Menumodifires> listMenumodifires = null;
		
		try {
			listMenumodifires = (ArrayList<Menumodifires>) objMenumodifiresService.loadByHql("from Menumodifires where menumodifireID in " + ids);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		for (Menumodifires menumodifires : listMenumodifires) {
			try {
				objMenumodifiresService.deleteRecord(menumodifires);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return onSelectMenu(map, menu, request, response);
	}
}
